package jqgridjavawrapper;

import java.util.ArrayList;
import org.omg.CORBA.PRIVATE_MEMBER;

/**
 * @author Isuru Madushanka Weerarathna
 * @contact uisurumadushanka89@gmail.com
 */
public class JQGridProperties implements JQPropertyMaker {

    private String ajaxGridOption = null;
    private String ajaxSelectOptions = null;
    private String cssAlternateRowClass = null;
    private boolean alternativeRowStripes = false;
    private boolean autoEncode = false;
    private boolean autoWidth = false;
    private String title = null;
    private int cellLayout = 5;
    private boolean cellEditable = false;
    private JQGridCellSubmitType cellSubmit = JQGridCellSubmitType.REMOTE;
    private String colmodelTemplate = null;
    private ArrayList<JQGridColumn> columns = new ArrayList<JQGridColumn>();
    private String dataString = null;
    private JQGridDataType dataType = JQGridDataType.DATA_JSON;
    private boolean deepEmpty = false;
    private boolean deselectAfterSort = true;
    private JQGridDirection direction = JQGridDirection.LEFT_TO_RIGHT;
    private String editURL = null;
    private String emptyRecordsText = null;
    private boolean expandColumnClick = true;
    private String expandColumn = null;
    private boolean footerRow = false;
    private boolean forceFit = false;
    private JQGridState gridState = JQGridState.VISIBLE;
    private boolean gridView = false;
    private JQGridGrouping grouping = null;
    private boolean headerTitles = false;
    private String height = null, width = null;
    private boolean hiddenGrid = false;
    private boolean hideGridButton = true;
    private boolean hoverRows = true;
    private boolean ignoreCase = false;
    private boolean loadOnce = true;
    private String loadText = null;
    private JQGridLoadingUIState loadUI = JQGridLoadingUIState.ENABLED;
    private JQGridRequestType requestType = JQGridRequestType.METHOD_GET;
    private JQGridMultiSelectKey multiSelectKey = JQGridMultiSelectKey.NONE;
    private boolean multiselectBoxOnly = false;
    private boolean multiSelect = false;
    private int multiSelectWidth = 20;
    private int startPage = 1;
    private JQGridAlignments pagerRecordPosition = JQGridAlignments.CENTER;
    private String cssForResizableClass = null;
    private int[] pageList = new int[] {10,20,30};
    private boolean rowNumbers = false;
    private int numOfRecordsPerPage = 10;
    private int columnWidthInRowNum = 25;
    private String scroll = null;
    private int scrollOffset = 18;
    private int scrollTimeout = 200;
    private boolean scrollRows = false;
    private boolean shrinkToFit = true;
    private boolean sortable = false;
    private String sortColumn = null;
    private JQGridSortType sortOrder = JQGridSortType.ASCENDING;
    private boolean subGrid = false;
    private JQGridSubGrid subGridSettings = null;
    private boolean topPager = false;

    public StringBuilder generateProperties() {
        StringBuilder scr = new StringBuilder();

        if (rowNumbers) scr.append("\nrownumbers: ").append(rowNumbers).append(',');
        if (pageList!=null) {
            scr.append("\nrowList: [");
            for (int i=0;i<pageList.length;i++) {
                if (i>0) scr.append(',');
                scr.append(pageList[i]);
            }
            scr.append(']');
        }
        if (numOfRecordsPerPage!=20) scr.append("\nrowNum: ").append(numOfRecordsPerPage).append(',');
        if (columnWidthInRowNum!=20) scr.append("\nrownumWidth: ").append(columnWidthInRowNum).append(',');
        if (scroll!=null) {
            scr.append("\nscroll: ").append(scroll).append(',');
        }
        if (scrollOffset!=18) scr.append("\nscrollOffset: ").append(scrollOffset).append(',');
        if (scrollTimeout!=200) scr.append("\nscrollTimeout: ").append(scrollTimeout).append(',');
        if (scrollRows) scr.append("\nscrollrows: ").append(scrollRows).append(',');
        if (!shrinkToFit) scr.append("\nshrinkToFit: ").append(shrinkToFit).append(',');
        if (sortable) scr.append("\nsortable: ").append(sortable).append(',');
        if (sortColumn!=null) scr.append("\nsortname: ").append(sortColumn).append(',');
        if (sortOrder!=JQGridSortType.ASCENDING) scr.append("\nsortorder: ").append(formatStr(sortOrder.toString())).append(',');
        if (subGrid) {
            scr.append("\nsubGrid: ").append(subGrid).append(',');
            scr.append(subGridSettings.generateProperties());
        }
        if (topPager) scr.append("\ntoppager: ").append(formatBool(topPager)).append(',');
        


        if (ajaxGridOption != null) {
            scr.append("\najaxGridOption: ").append(ajaxGridOption).append(',');
        }
        if (ajaxSelectOptions != null) {
            scr.append("\najaxSelectOptions: ").append(ajaxSelectOptions).append(',');
        }
        if (cssAlternateRowClass != null) {
            scr.append("\naltclass: ").append(formatStr(cssAlternateRowClass)).append(',');
        }
        if (!alternativeRowStripes) {
            scr.append("\naltRows: ").append(formatBool(alternativeRowStripes)).append(',');
        }
        if (!autoEncode) {
            scr.append("\nautoencode: ").append(formatBool(autoEncode)).append(',');
        }
        if (!autoWidth) {
            scr.append("\nautowidth: ").append(formatBool(autoWidth)).append(',');
        }

        scr.append("\ncaption: '").append((title == null ? "" : title)).append(',');

        if (cellLayout != 5) {
            scr.append("\ncellLayout: ").append(cellLayout).append(',');
        }
        if (cellEditable != false) {
            scr.append("\ncellEdit: ").append(formatBool(cellEditable)).append(',');
        }
        if (cellSubmit != JQGridCellSubmitType.REMOTE) {
            scr.append("\ncellsubmit: ").append(cellSubmit.toString()).append(',');
        }
        if (colmodelTemplate != null) {
            scr.append("\ncmTemplate: ").append(colmodelTemplate).append(',');
        }
        if (dataString != null) {
            scr.append("\ndatastr: ").append("\"").append(dataString).append("\",");
        }
        if (dataType != null) {
            scr.append("\ndatatype: ").append(formatStr(dataType.toString())).append(',');
        }
        if (deepEmpty) {
            scr.append("\ndeepempty: ").append(formatBool(deepEmpty)).append(',');
        }
        if (!deselectAfterSort) {
            scr.append("\ndeselectAfterSort: ").append(formatBool(deselectAfterSort)).append(',');
        }
        if (direction != JQGridDirection.LEFT_TO_RIGHT) {
            scr.append("\ndirection: ").append(formatStr(direction.toString())).append(',');
        }
        if (editURL != null) {
            scr.append("\nediturl: ").append(formatStr(editURL)).append(',');
        }
        if (emptyRecordsText != null) {
            scr.append("\nemptyrecords: ").append(formatStr(emptyRecordsText)).append(',');
        }
        if (!expandColumnClick) {
            scr.append("\nexpandColClick: ").append(formatBool(expandColumnClick)).append(',');
        }
        if (expandColumn != null) {
            scr.append("\nExpandColumn: ").append(formatStr(expandColumn)).append(',');
        }
        if (footerRow) {
            scr.append("\nfooterrow: ").append(formatBool(footerRow)).append(',');
        }
        if (forceFit) {
            scr.append("\nforcefit: ").append(formatBool(forceFit)).append(',');
        }
        if (gridState != JQGridState.VISIBLE) {
            scr.append("\ngridstate: ").append(formatStr(gridState.toString())).append(',');
        }
        if (gridView) {
            scr.append("\ngridview: ").append(formatBool(gridView)).append(',');
        }
        if (grouping != null) {
            scr.append("\ngrouping: true, \n groupingView : { \n").append(grouping.generateProperties()).append("\n }");
        }
        if (headerTitles) {
            scr.append("\nheadertitles: ").append(formatBool(headerTitles)).append(',');
        }
        if (width != null) {
            scr.append("\nwidth: ").append((isNumeric(width) ? width : formatStr(width))).append(',');
        }
        if (height != null) {
            scr.append("\nwidth: ").append((isNumeric(height) ? height : formatStr(height))).append(',');
        }
        if (hiddenGrid) {
            scr.append("\nhiddengrid: ").append(formatBool(hiddenGrid)).append(',');
        }
        if (!hideGridButton) {
            scr.append("\nhidegrid: ").append(formatBool(hideGridButton)).append(',');
        }
        if (!hoverRows) {
            scr.append("\nhoverrows: ").append(formatBool(hoverRows)).append(',');
        }
        if (ignoreCase) {
            scr.append("\nignoreCase: ").append(formatBool(ignoreCase)).append(',');
        }
        if (loadOnce) {
            scr.append("\nloadonce: ").append(formatBool(loadOnce)).append(',');
        }
        if (loadText != null) {
            scr.append("\nloadtext: ").append(formatStr(loadText)).append(',');
        }
        if (loadUI != JQGridLoadingUIState.ENABLED) {
            scr.append("\nloadui: ").append(formatStr(loadUI.toString())).append(',');
        }
        if (requestType != JQGridRequestType.METHOD_GET) {
            scr.append("\nmtype: ").append(formatStr(requestType.toString())).append(',');
        }
        if (multiSelectKey != JQGridMultiSelectKey.NONE) {
            scr.append("\nmultikey: ").append(formatStr(multiSelectKey.toString())).append(',');
        }
        if (multiselectBoxOnly) {
            scr.append("\nmultiboxonly: ").append(formatBool(multiselectBoxOnly)).append(',');
        }
        if (multiSelect) {
            scr.append("\nmultiselect: ").append(formatBool(multiSelect)).append(',');
        }
        if (multiSelectWidth != 20) {
            scr.append("\nmultiselectWidth: ").append(multiSelectWidth).append(',');
        }
        if (startPage > 1) {
            scr.append("\npage: ").append(startPage).append(',');
        }
        if (pagerRecordPosition!=JQGridAlignments.CENTER) scr.append("\nrecordpos: ").append(formatStr(pagerRecordPosition.toString())).append(',');
        if (cssForResizableClass!=null) scr.append("\nresizeclass: ").append(formatStr(cssForResizableClass)).append(',');
        


        return scr;
    }

    public String getDataString() {
        return dataString;
    }

    public JQGridDataType getDataType() {
        return dataType;
    }

    public JQGridDirection getDirection() {
        return direction;
    }

    public void setDataString(String dataString) {
        this.dataString = dataString;
    }

    public void setDataType(JQGridDataType dataType) {
        this.dataType = dataType;
    }

    public void setDeepEmpty(boolean deepEmpty) {
        this.deepEmpty = deepEmpty;
    }

    public void setDeselectAfterSort(boolean deselectAfterSort) {
        this.deselectAfterSort = deselectAfterSort;
    }

    public void setDirection(JQGridDirection direction) {
        this.direction = direction;
    }

    public boolean isDeepEmpty() {
        return deepEmpty;
    }

    public boolean isDeselectAfterSort() {
        return deselectAfterSort;
    }

    public void addColumn(JQGridColumn column) {
        columns.add(column);
    }

    public String getColmodelTemplate() {
        return colmodelTemplate;
    }

    public ArrayList<JQGridColumn> getColumns() {
        return columns;
    }

    public void setColmodelTemplate(String colmodelTemplate) {
        this.colmodelTemplate = colmodelTemplate;
    }

    public void setColumns(ArrayList<JQGridColumn> columns) {
        this.columns = columns;
    }

    public JQGridCellSubmitType getCellSubmit() {
        return cellSubmit;
    }

    public void setCellSubmit(JQGridCellSubmitType cellSubmit) {
        this.cellSubmit = cellSubmit;
    }

    public boolean isCellEditable() {
        return cellEditable;
    }

    public void setCellEditable(boolean cellEditable) {
        this.cellEditable = cellEditable;
    }

    public void setCellLayout(int cellLayout) {
        this.cellLayout = cellLayout;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getCellLayout() {
        return cellLayout;
    }

    public String getTitle() {
        return title;
    }

    public boolean isAlternativeRowStripes() {
        return alternativeRowStripes;
    }

    public boolean isAutoEncode() {
        return autoEncode;
    }

    public boolean isAutoWidth() {
        return autoWidth;
    }

    public String getAjaxGridOption() {
        return ajaxGridOption;
    }

    public String getAjaxSelectOptions() {
        return ajaxSelectOptions;
    }

    public String getCssAlternateRowClass() {
        return cssAlternateRowClass;
    }

    public void setAjaxGridOption(String ajaxGridOption) {
        this.ajaxGridOption = ajaxGridOption;
    }

    public void setAjaxSelectOptions(String ajaxSelectOptions) {
        this.ajaxSelectOptions = ajaxSelectOptions;
    }

    public void setCssAlternateRowClass(String cssAlternateRowClass) {
        this.cssAlternateRowClass = cssAlternateRowClass;
    }

    public void setAlternativeRowStripes(boolean alternativeRowStripes) {
        this.alternativeRowStripes = alternativeRowStripes;
    }

    public void setAutoEncode(boolean autoEncode) {
        this.autoEncode = autoEncode;
    }

    public void setAutoWidth(boolean autoWidth) {
        this.autoWidth = autoWidth;
    }

    private String formatBool(boolean value) {
        return (value ? "true" : "false");
    }

    private String formatStr(String value) {
        return "'" + value + "'";
    }

    private boolean isNumeric(String text) {
        for (int i = 0; i < text.length(); i++) {
            if (!Character.isDigit(text.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
